<?php

namespace App\Http\Controllers;

use App\Http\Requests\ArticleAddRequest;
use App\Http\Requests\GameAddRequest;
use App\Http\Requests\ListRequest;
use App\Models\Article;
use App\Models\Game;

class ManageConsultController extends Controller
{
    public function articleList(ListRequest $request){
        $pageSize = $request->post('page_size',1);
        $filters = $request['filters'];
        $articleModel = new Article();
        $data = $articleModel->select([
            'id',
            'title',
            'is_top',
            'is_hot',
            'article_type_id as article_type_str',
            'id as key',
            'enable_status'
        ])->where('status',1);
        if(!empty($filters)){
            if(!empty($filters['title'])){
              $data = $data->where('title','like','%'.$filters['title'].'%');
            }
            if(!empty($filters['article_type_id'])){
                $data = $data->where('article_type_id',$filters['article_type_id']);
            }

            if(!empty($filters['is_top'])){
                $data = $data->where('is_top',$filters['is_top']);
            }

            if(!empty($filters['is_hot'])){
                $data = $data->where('is_hot',$filters['is_hot']);
            }

            if(!empty($filters['enable_status'])){
                $data = $data->where('enable_status',$filters['enable_status']);
            }

        }
        $data = $data->orderBy('id','desc')
            ->paginate($pageSize);
        return c_return(0,get_simple_page($data));
    }

    public function articleAdd(ArticleAddRequest $request){
        $data = [
            'title' => $request->post('title'),
            'synopsis' => $request->post('synopsis'),
            'show_img_id' => $request->post('show_img_id'),
            'author' => $request->post('author'),
            'content' => $request->post('content'),
            'article_type_id' => $request->post('article_type_id'),
            // default
            'created_at'=>date('Y-m-d H:i:s',time()),
            'owner_id' => 1,
            'status' =>1
        ];

        $articleModel = new Article();

        $id = $articleModel->insertGetId($data);
        return c_return(0,[
            'id'=> $id
        ]);

    }

    public function articleDel(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        try {
            $articleModel->where('id',$id)->where('status',1)->update([
                'status' => 2,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }

    public function articleEnable(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        try {
            $articleModel->where('id',$id)->where('enable_status',2)->update([
                'enable_status' => 1,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }

    public function articleDisable(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        try {
            $articleModel->where('id',$id)->where('enable_status',1)->update([
                'enable_status' => 2,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }

    public function articleHot(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        try {
            $articleModel->where('id',$id)->update([
                'is_hot' => 1,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }

    public function articleCancelHot(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        try {
            $articleModel->where('id',$id)->update([
                'is_hot' => 2,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }

    public function articleTop(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        try {
            $articleModel->where('id',$id)->update([
                'is_top' => 1,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }

    public function articleCancelTop(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        try {
            $articleModel->where('id',$id)->update([
                'is_top' => 2,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }


    public function articleUpdate(){
        $request = request();
        $id = $request->post('id',0);
        $data = [
            'title' => $request->post('title'),
            'synopsis' => $request->post('synopsis'),
            'content' => $request->post('content'),
            'author' => $request->post('author'),
            'article_type_id' => $request->post('article_type_id'),
            'show_img_id' => $request->post('show_img_id'),
            // default
            'updated_at'=>date('Y-m-d H:i:s',time()),
        ];
        $articleModel = new Article();
        try {
            $articleModel->where('id',$id)->where('status',1)->update($data);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }

    }

    public function articleInfo(){
        $id = request()->post('id',0);
        $articleModel = new Article();
        $data = $articleModel
            ->select([
                'id',
                'id as key',
                'title',
                'author',
                'synopsis',
                'is_top',
                'is_hot',
                'content',
                'article_type_id',
                'show_img_id',
                'enable_status',
                'created_at',
                'article_type_id as article_type_str'
            ])
            ->where('status',1)
            ->where('id',$id)
            ->first();
        return c_return(0,$data);
    }

    public function gameList(ListRequest $request){
        $pageSize = $request->post('page_size',1);
        $gameModel = new Game();
        $filters = $request['filters'];
        $data = $gameModel->select([
            'id',
            'name',
            'game_type_id as game_type_str',
            'os',
            'id as key',
            'enable_status'
        ])->where('status',1);

        if(!empty($filters)){
            if(!empty($filters['name'])){
                $data = $data->where('name','like','%'.$filters['name'].'%');
            }
            if(!empty($filters['os'])){
                $data = $data->where('os','like','%'.$filters['os'].'%');
            }
            if(!empty($filters['game_type_id'])){
                $data = $data->where('game_type_id',$filters['game_type_id']);
            }

            if(!empty($filters['enable_status'])){
                $data = $data->where('enable_status',$filters['enable_status']);
            }

        }
        $data = $data->orderBy('id','desc')
        ->paginate($pageSize);
        return c_return(0,get_simple_page($data));
    }

    public function gameAdd(GameAddRequest $request){
        $data = [
            'name' => $request->post('name'),
            'synopsis' => $request->post('synopsis'),
            'content' => $request->post('content'),
            'show_img_id' => $request->post('show_img_id'),
            'carousel_ids' => $request->post('carousel_ids'),
            'game_type_id' => $request->post('game_type_id'),
            'os' => $request->post('os'),
            'version' => $request->post('version'),
            'size' => $request->post('size'),
            'update' => $request->post('update'),
            'a_download' => $request->post('a_download'),
            'ios_download' => $request->post('ios_download'),
            // default
            'created_at'=>date('Y-m-d H:i:s',time()),
            'owner_id' => 1,
            'status' =>1
        ];
        // 处理轮播图
        $data['carousel_ids'] = implode(',',$data['carousel_ids']);
        $gamesModel = new Game();
        $id = $gamesModel->insertGetId($data);
        return c_return(0,[
            'id'=> $id
        ]);
    }



    public function gameUpdate(){
        $request = request();
        $data = [
            'id' => $request->post('id'),
            'name' => $request->post('name'),
            'synopsis' => $request->post('synopsis'),
            'content' => $request->post('content'),
            'show_img_id' => $request->post('show_img_id'),
            'carousel_ids' => $request->post('carousel_ids'),
            'game_type_id' => $request->post('game_type_id'),
            'os' => $request->post('os'),
            'version' => $request->post('version'),
            'size' => $request->post('size'),
            'update' => $request->post('update'),
            'a_download' => $request->post('a_download'),
            'ios_download' => $request->post('ios_download'),
        ];
        // 处理轮播图
        $data['carousel_ids'] = implode(',',$data['carousel_ids']);
        $gamesModel = new Game();
        $id = $gamesModel->where('id',$data['id'])->update($data);
        return c_return(0,[
            'id'=> $id
        ]);
    }

    public function gameInfo(){
        $id = request()->post('id');
        $gameModel = new Game();
        $data = $gameModel
            ->select([
                'id',
                'id as key',
                'name',
                'synopsis',
                'content',
                'show_img_id',
                'carousel_ids as carousel_ids_arr',
                'game_type_id',
                'game_type_id as game_type_str',
                'os',
                'version',
                'size',
                'update',
                'a_download',
                'enable_status',
                'ios_download',
            ])
            ->where('id',$id)
            ->where('status',1)
            ->first();
        return c_return(0,$data);
    }

    public function gameDel(){
        $id = request()->post('id',0);
        $gameModel = new Game();
        try {
            $gameModel->where('id',$id)->where('status',1)->update([
                'status' => 2,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }

    public function gameEnable(){
        $id = request()->post('id',0);
        $gameModel = new Game();
        try {
            $gameModel->where('id',$id)->where('enable_status',2)->update([
                'enable_status' => 1,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }

    public function gameDisable(){
        $id = request()->post('id',0);
        $gameModel = new Game();
        try {
            $gameModel->where('id',$id)->where('enable_status',1)->update([
                'enable_status' => 2,
                'updated_at' => date('Y-m-d H:i:s',time())
            ]);
            return c_return(0,null);
        }catch (\Exception $exception){
            cp_return(__('message.operation failed'));
        }
    }
}
